package PangView;

import PangController.GameEngine;
import PangController.GameOverException;
import sun.awt.event.IgnorePaintEvent;
import sun.rmi.runtime.Log;

import javax.swing.*;
import java.awt.*;

/**
 * Created by baraaorabi on 5/11/15.
 */
public class GameplayPanel extends JPanel {
    final int FRAMES_PER_SECOND = 25;
    final long SKIP_TICKS = 1000 / FRAMES_PER_SECOND;

    final  Dimension dim = new Dimension (1100, 730);//dimension of the game screen
    private boolean gameIsRunnning;
    private GameEngine gameEngine;

    GameplayPanel(){
        setPreferredSize(dim);
        gameEngine = new GameEngine(1);
        setBackground(Color.BLUE);
        this.setIgnoreRepaint(true);
    }

    @Override
    public void paint(Graphics page)
    {
        super.paint(page);
        System.out.printf("abc");
        gameEngine.drawObjects(page);
    }


    void playGame(MainFrame mainFrame){
        gameIsRunnning = true;
        double timeElapesed = 0;
        double timeNeededToUpdate = 0;
        while (gameIsRunnning){
            timeElapesed = System.currentTimeMillis();

            try {
                gameEngine.tick();
            } catch (GameOverException e) {
                e.printStackTrace();
                gameIsRunnning = false;
            }
            revalidate();
            this.repaint();
            timeElapesed = System.currentTimeMillis() - timeElapesed;

            if (timeElapesed < SKIP_TICKS){
                try {
                    //Thread.sleep(0);
                    //Thread.sleep((long) (SKIP_TICKS - timeElapesed));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {

            }
//            mainFrame.repaint();


        }

        System.out.println("GAMEOVER!");

    }

}
